﻿#if UNITY_EDITOR

using System.Collections.Generic;
using UnityEditor; 
using UnityEngine;

namespace XFGameFramework.ScriptableObjectSystem
{ 
    public class CustomConfigDataIDGUI : CustomPropertyGUI
    {
 
        public CustomConfigDataIDGUI() : base(true) {}

        public override void OnPropertyGUI(SerializedProperty serializedProperty)
        { 
            EditorGUILayout.LabelField(string.Empty, "id(唯一标识,不可重复)", "BoldLabel");
            int id = serializedProperty.intValue;
            EditorGUI.BeginChangeCheck();
            id = EditorGUILayout.DelayedIntField("Id", id);
            if (EditorGUI.EndChangeCheck())
            {  
                // 这里只需要判断当前配表中是否有这个新的id ， 其他的拓展包判断的是所有的配置 这个不需要
                ScriptableObjectData data = serializedProperty.serializedObject.targetObject as ScriptableObjectData;
                ScriptableObjectData configData = data.parent.GetScriptableObjectData(id);  
                if (configData == null)
                {
                    serializedProperty.intValue = id;
                    serializedProperty.serializedObject.ApplyModifiedProperties();
                    ScriptableObjectData scriptableConfigData = serializedProperty.serializedObject.targetObject as ScriptableObjectData;
                    if (scriptableConfigData != null) scriptableConfigData.Rename();
                    if (scriptableConfigData != null && scriptableConfigData.parent != null)
                        scriptableConfigData.parent.ClearCache();
                }
                else
                {
                    string message = string.Format("id:{0}已被占用,请使用其他数字来作为id!", id);
                    EditorWindow.focusedWindow.ShowNotification(new GUIContent(message)); 
                }
            }

        }
         
    
    } 
}

#endif
